package Util::Taobao::StoresCreate;
use Moose;
use namespace::autoclean;
use JSON;
use WWW::Curl::Easy;
use HTTP::Response;
use Digest::MD5 qw(md5_hex);
use URI::Escape;
use Encode;
use Data::Dumper;
sub refresh_token {
        my ($self, $code, $appKey, $clientSecret, $session, $oauthUri) = @_;
        my $post_fields = "code=$code&grant_type=refresh_token&client_id=$appKey&client_secret=$clientSecret&refresh_token=$session";

        my $curl = WWW::Curl::Easy->new;
        $curl->setopt(CURLOPT_HEADER,1);
        $curl->setopt(CURLOPT_POSTFIELDS, $post_fields);
        $curl->setopt(CURLOPT_URL, $oauthUri);

        my $response;
        $curl->setopt(CURLOPT_WRITEDATA,\$response);

        my $retcode = $curl->perform;

        if ($retcode == 0) {
		$response = HTTP::Response->parse($response);
		my $xml = $response->decoded_content;
		my $json = from_json($xml);
                return $json->{'access_token'};

	}
	return "0"; 

}

sub get_stores {
	my ($self, $session, $code, $appKey, $clientSecret, $gatewayUrl, $redirectUri, $oauthUri, $apiVersion, $storeSearchModel, $refresh_token) = @_;
	my $num = 0;

	my %resData;
	my $returncode = 0;
	my $page_no = 1;
	my $page_size = 100;
	my $count = $page_size;
	while($count == $page_size) {
		$session = $self->refresh_token($code, $appKey, $clientSecret, $refresh_token, $oauthUri);
		if($session eq '0'){
			$returncode = 1;
			last;
		}
		$count = 0;
		my %sysParams;
		$sysParams{"app_key"} = $appKey;
		$sysParams{"v"} = $apiVersion;
		$sysParams{"format"} = "json";
		$sysParams{"sign_method"} = "md5";
		$sysParams{"partner_id"} = "top-apitools";
		$sysParams{"page_no"} = "$page_no";
		$sysParams{"page_size"} = "$page_size";
		$sysParams{"method"} = "taobao.items.onsale.get";
		my $time = `date +"%Y-%m-%d %H:%M:%S"`;
		chomp($time);
		$sysParams{"timestamp"} = $time;
		$sysParams{"fields"} = "num_iid,title,pic_url";

		$sysParams{"session"} = $session;

		my $stringToBeSigned = $clientSecret;
		foreach my $key (sort(keys(%sysParams))) {
			$stringToBeSigned .= "$key$sysParams{$key}";
		}
		$stringToBeSigned .= $clientSecret;
		my $md5 = uc(md5_hex($stringToBeSigned));
		$sysParams{"sign"} = $md5;

		my $requestUrl = $gatewayUrl . "?";
		foreach my $key (sort(keys(%sysParams))) {
			$requestUrl .= "$key=" . uri_escape($sysParams{$key}) . "&";
		}

		my $curl_new = WWW::Curl::Easy->new;
		$curl_new->setopt(CURLOPT_HEADER,1);
		$curl_new->setopt(CURLOPT_URL, $requestUrl);

		my $response_new;
		$curl_new->setopt(CURLOPT_WRITEDATA,\$response_new);
		my $retcode_new = $curl_new->perform;
		
		#TODO
		$retcode_new = 0;
		if ($retcode_new == 0) {
			$response_new = HTTP::Response->parse($response_new);

			my $xml_new = $response_new->decoded_content;
			my $json_new = from_json($xml_new);

			my $items = $json_new->{'items_onsale_get_response'}->{'items'}->{'item'};
			
			for my $item (@$items) {
				$resData{$num}{'num_iid'} =  $item->{'num_iid'};	
				$resData{$num}{'pic_url'} =  $item->{'pic_url'};	
				$resData{$num}{'title'} =  $item->{'title'};	
				$resData{$num}{'title'} = encode('UTF-8', $resData{$num}{'title'});

				my @stores = $storeSearchModel->get_stores_by_numiid($resData{$num}{'num_iid'});
				if(scalar @stores > 0) {
					$resData{$num}{'exist'} = 'Y';
					$resData{$num}{'id'} = $stores[0]->id;
				} else {
					$resData{$num}{'exist'} = 'N';
				}
				$num ++;
				$count ++;
			}
			$returncode = 0;
		} else {
			$returncode  = 1;
			last;
		}
		$page_no ++;
	}

	return (\%resData, $returncode);
}
__PACKAGE__->meta->make_immutable;

1;
